import 'package:flutter/material.dart';
import 'package:forui/forui.dart';
import 'package:get/get.dart';
import 'package:home_box/pages/medicine/MedicineVM/medicine_add_vm.dart';

import '../../../../constants.dart';
///药品用量
class MedicineEveryTimeCount extends StatelessWidget {
  const MedicineEveryTimeCount({super.key});

  @override
  Widget build(BuildContext context) {
    return  GetBuilder<MedicineVM>(
      tag: 'MedicineVM',
      builder: (controller) {
        return Container(
          margin: EdgeInsets.only(top: 10),
          height: 30,
          child: GestureDetector(
            onTap: () {
              _showDosage(context, controller);
            },
            child: Row(
              children: [
                Text('每次用量：'),
                Expanded(
                  child: TextField(
                    textAlignVertical: TextAlignVertical.center,
                    textAlign: TextAlign.start,
                    controller: controller.dosageController,
                    decoration: InputDecoration(
                      hintText: '请选择每次用量',
                      hintStyle: TextStyle(fontSize: 14),
                      border: InputBorder.none,
                      isDense: true,
                    ),
                    enabled: false,
                    style: TextStyle(color: Colors.black),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

Future<int?> _showDosage(BuildContext context, MedicineVM vm) {
  FPickerController controller = FPickerController(
    initialIndexes: [vm.dosageIndex.value, vm.dosageUnitIndex.value],
  );

  return showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return SizedBox(
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            SizedBox(
              width: MediaQuery.of(context).size.width,
              height: 60,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                    left: 15,
                    child: SizedBox(
                      width: 15,
                      height: 15,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Image.asset('images/close.png'),
                      ),
                    ),
                  ),
                  Positioned(
                    child: Text(
                      '每次用量',
                      style: TextStyle(fontSize: 16, color: Colors.blue),
                    ),
                  ),
                  Positioned(
                    right: 10,
                    child: GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                        int indexDosage =
                            controller.value[0] % (Constants.dosage.length);

                        int indexUnit =
                            controller.value[1] % (Constants.dosageUnit.length);
                        vm.dosageIndex.value = indexDosage;
                        vm.dosageUnitIndex.value = indexUnit;
                        vm.dosageController.text =
                            Constants.dosage[indexDosage].toString() +
                            Constants.dosageUnit[indexUnit];
                        vm.updateDosage(Constants.dosage[indexDosage]);
                      },
                      child: Container(
                        margin: EdgeInsets.all(20.0),
                        child: Text('确认', style: TextStyle(color: Colors.blue)),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Center(
              child: SizedBox(
                width: 80,
                height: 300,
                child: FPicker(
                  controller: controller,
                  children: [
                    FPickerWheel(
                      flex: 1,
                      loop: true,
                      children:
                          Constants.dosage.map((value) {
                            return Text('$value');
                          }).toList(),
                    ),
                    FPickerWheel(
                      flex: 1,
                      loop: true,
                      children:
                          Constants.dosageUnit.map((value) {
                            return Text(value);
                          }).toList(),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      );
    },
  );
}
